package com.webull.information.center.carwler.common.util.news.detection;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.framework.util.UtilDate;
import com.webull.information.center.carwler.common.model.NewsDetection;
import com.webull.information.center.carwler.common.model.NewsDetection.DetectionResult;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;

@Component
public class DetectionInvestingList implements DetectionList {
	private final Logger logger = LogManager.getLogger(getClass());
	private @Autowired
    HttpJsoupClient htmlPrase;

	private static final String R_URL = "https://www.investing.com/equities/{0}-news";
	private static final String R_URL2 = "https://www.investing.com/etfs/{0}-news";

	// String url2 = "https://www.investing.com/equities/hsbc-holdings-news";
	// url2 = "https://www.investing.com/equities/ping-an-bank-a-news;
	// https://www.investing.com/etfs/spdr-s-p-500-news
	@Override
	public List<NewsDetection.DetectionResult> parseNewsList(NewsDetection.Detection ticker) {
		String url = StringUtils.replaceOnce(R_URL, "{0}", ticker.getsSymbol());
		if ("etfs".equals(ticker.getsExchange())) {
			url = StringUtils.replaceOnce(R_URL2, "{0}", ticker.getsSymbol());
		}
		Document doc = htmlPrase.parseDocumentFromUrl(url, true);

		// url = "https://www.investing.com/equities/hsbc-holdings-news";
		// Connection connection = Jsoup.connect(url)
		// .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
		// (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
		// // .header("x-client-data",
		// "CIq2yQEIpbbJAQjEtskBCP2VygEIwpjKAQjwnMoB")
		// .header("x-client-data", RandomStringUtils.randomAlphanumeric(40));
		//
		// // connection.proxy("127.0.0.1", 1080);
		//
		// Document doc = null;
		// try {
		// doc = connection.timeout(10000).get();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		if (doc == null) {
			logger.warn("DetectionInvestingList parseNewsList parse url:{} ,doc is null!!", url);
			return null;
		}
		return parseNewsList(ticker, doc);
	}

	private List<NewsDetection.DetectionResult> parseNewsList(NewsDetection.Detection ticker, Document doc) {
		List<NewsDetection.DetectionResult> result = new ArrayList<>();

		// 美股新闻页面mediumTitle1
		Optional.ofNullable(doc.select("div.mediumTitle1>article.articleItem"))//
				.ifPresent(v -> {
					v.forEach(v0 -> {
						try {
							NewsDetection.DetectionResult item = new NewsDetection.DetectionResult(ticker);
							Optional.ofNullable(v0.select("div.textDiv>a").first()).ifPresent(a0 -> {
								item.setUrl(StringUtils.stripToNull(a0.absUrl("href")));
								if (item.getUrl() != null)
									item.setUrlMd5(HttpJsoupClient.finishingMd5Url(item.getUrl() + item.getsSource()));
								item.setTitle(a0.text());
							});

							Optional.ofNullable(v0.select("span.date").first()).map(v1 -> StringUtils.strip(v1.text()))
									.ifPresent(s0 -> {
										// - Jul 13, 2015

										Integer numn = Integer.valueOf(StringUtils.removePattern(s0, "\\D"));

										if (StringUtils.contains(s0, "minute")) {
											item.setNewsTime(DateUtils.addMinutes(new Date(), -numn));//
										} else if (StringUtils.contains(s0, "hour")) {
											item.setNewsTime(DateUtils.addHours(new Date(), -numn));//
										} else {
											// Feb 15, 2017
											Pattern pattern1 = Pattern.compile("[a-zA-z]{3,}");
											Matcher matcher = pattern1.matcher(s0);

											if (matcher.find()) {
												try {
													item.setNewsTime(UtilDate.parse(
															StringUtils.substring(s0, matcher.start()).trim(),
															Locale.ENGLISH, UtilDate.getTimeZone(0), "MMM d, yyyy"));
												} catch (Exception e) {

												}
											}

										}
										// System.out.println(StringUtils.removePattern(s0.text(),
										// "\\s*[-]{1,}\\s*"));
									});
							// (v2 ->
							// StringUtils.stripToNull(v2.absUrl("href"))).orElse(null));//
							if (item.getNewsTime() == null)
								item.setNewsTime(new Date());
							result.add(item);
						} catch (Exception e) {
							logger.warn("DetectionBloombergList error:{}", e);
						}
					});
				});

		return result;
	}

	public static void main(String[] args) {
		DetectionInvestingList ll = new DetectionInvestingList();
		NewsDetection.Detection ticker = new NewsDetection.Detection();
		ticker.setTickerId(129384);
		ticker.setsExchange("SS");
		ticker.setsSource("ABC");
		ticker.setsSymbol("000001");
		List<DetectionResult> a = ll.parseNewsList(ticker);
		for (int i = 0; i < a.size(); i++) {
			System.out.println(a.get(i).getTitle());
		}
		System.out.println(a);
	}

}
